/*
Copyright (c) 2008, Daniel Guerrero Martínez
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
	-Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.
	-Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.
	-Neither the name of the <ORGANIZATION> nor the names of its contributors may
	be used to endorse or promote products derived from this software without
	specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
 */


package Imagenes;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;


public class LienzoInterno extends javax.swing.JInternalFrame {


    public LienzoInterno(BarraHerramientas H,Dimension D,Color C) {
        
        Her = H;
        initComponents();

        BufferedImage fondo = new BufferedImage((int)D.getWidth(),(int)D.getHeight(),BufferedImage.TYPE_INT_RGB);
        Graphics2D g = fondo.createGraphics();
        g.setColor(C);
        g.fillRect(0,0, (int)D.getWidth(),(int)D.getHeight());

        lienzo = new Lienzo(H,fondo);

        scrollp.add(lienzo);

        lienzo.setSize(D);
        scrollp.setPreferredSize(D);

        scroll.setSize(D);


        this.setLocation(200,0);
        this.show();
    }

    public LienzoInterno(BarraHerramientas H,BufferedImage I){
        lienzo = new Lienzo(H, I);
        Her = H;
        initComponents();
        if(I!=null){
            lienzo.setSize( I.getWidth(),I.getHeight());
            scrollp.setPreferredSize(new Dimension(I.getWidth(),I.getHeight()));
        }
        scrollp.add(lienzo);
        this.setLocation(200,0);
        this.show();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textField1 = new java.awt.TextField();
        scroll = new javax.swing.JScrollPane();
        scrollp = new javax.swing.JPanel();
        barramenu = new javax.swing.JMenuBar();
        M_archivo = new javax.swing.JMenu();
        B_exportar = new javax.swing.JMenuItem();
        B_imagen = new javax.swing.JMenu();
        B_combinar = new javax.swing.JMenuItem();
        B_efectos = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        FormListener formListener = new FormListener();

        textField1.setText("textField1");

        setBackground(new java.awt.Color(254, 254, 254));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setMinimumSize(new java.awt.Dimension(20, 20));
        setPreferredSize(new java.awt.Dimension(400, 400));
        setVisible(true);
        addInternalFrameListener(formListener);
        addComponentListener(formListener);

        scrollp.setLayout(null);
        scroll.setViewportView(scrollp);

        getContentPane().add(scroll, java.awt.BorderLayout.CENTER);

        M_archivo.setText("Archivo");

        B_exportar.setText("Exportar a imagen");
        B_exportar.addActionListener(formListener);
        M_archivo.add(B_exportar);

        barramenu.add(M_archivo);

        B_imagen.setText("Imagen");

        B_combinar.setText("Combinar Imagen y trazos");
        B_combinar.addActionListener(formListener);
        B_imagen.add(B_combinar);

        B_efectos.setText("Efectos");
        B_efectos.addActionListener(formListener);
        B_imagen.add(B_efectos);

        barramenu.add(B_imagen);
        barramenu.add(jMenu2);

        setJMenuBar(barramenu);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.ComponentListener, javax.swing.event.InternalFrameListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == B_exportar) {
                LienzoInterno.this.exportar_imagen(evt);
            }
            else if (evt.getSource() == B_combinar) {
                LienzoInterno.this.combinar_imagen(evt);
            }
            else if (evt.getSource() == B_efectos) {
                LienzoInterno.this.Filtros(evt);
            }
        }

        public void componentHidden(java.awt.event.ComponentEvent evt) {
        }

        public void componentMoved(java.awt.event.ComponentEvent evt) {
        }

        public void componentResized(java.awt.event.ComponentEvent evt) {
            if (evt.getSource() == LienzoInterno.this) {
                LienzoInterno.this.centrando_lienzo(evt);
            }
        }

        public void componentShown(java.awt.event.ComponentEvent evt) {
        }

        public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            if (evt.getSource() == LienzoInterno.this) {
                LienzoInterno.this.seleccionamos_ventana(evt);
            }
        }

        public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
        }

        public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    private void seleccionamos_ventana(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_seleccionamos_ventana
        Her.mod_point_gradiente(this.getSize().width, this.getSize().height);
    }//GEN-LAST:event_seleccionamos_ventana

    private void centrando_lienzo(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_centrando_lienzo
         lienzo.setLocation((scrollp.getWidth()/2)-(lienzo.getWidth()/2),(scrollp.getHeight()/2)-(lienzo.getHeight()/2));
}//GEN-LAST:event_centrando_lienzo

    private void combinar_imagen(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combinar_imagen
        lienzo.Combinar();
    }//GEN-LAST:event_combinar_imagen

    private void Filtros(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Filtros
        Filtros dialogo = new Filtros(null,true,lienzo.get_original());
        dialogo.setVisible(true);
        lienzo.actualizar_paint();
    }//GEN-LAST:event_Filtros

    private void exportar_imagen(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportar_imagen
        JFileChooser dig = new javax.swing.JFileChooser();
        dig.setDialogType(JFileChooser.SAVE_DIALOG);
        dig.setDialogTitle("Esportar a Imagen");
        dig.setMultiSelectionEnabled(false);
        dig.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif","png"));
        int resp = dig.showOpenDialog(this);
        if(resp == JFileChooser.APPROVE_OPTION){
            try {
                File f = dig.getSelectedFile();
                ImageIO.write(lienzo.get_imagen_combinada(),"jpg",f);
            } catch (IOException ex) {
                javax.swing.JOptionPane err = new javax.swing.JOptionPane("El archivo no pudo salvarse", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_exportar_imagen

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem B_combinar;
    private javax.swing.JMenuItem B_efectos;
    private javax.swing.JMenuItem B_exportar;
    private javax.swing.JMenu B_imagen;
    private javax.swing.JMenu M_archivo;
    private javax.swing.JMenuBar barramenu;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JScrollPane scroll;
    private javax.swing.JPanel scrollp;
    private java.awt.TextField textField1;
    // End of variables declaration//GEN-END:variables
    private Lienzo lienzo;
    private BarraHerramientas Her;
}
